﻿Ext.define('Oa.GXPackageGrid', {
    extend: 'Ext.grid.Panel',
    alias: 'Widget.GXPackageGrid',

    // override
    initComponent: function () {
        this.columns = [{
            header: "财富套餐", width: 200, dataIndex: 'Name'
        }, {
            header: "是否有效", flex: 1, dataIndex: 'IsValid',
            renderer: function (value) {
                if (value == 1) {
                    return "是";
                } else {
                    return "否";
                }
            }
        }, {
            header: "备注", width: 200, dataIndex: 'Remarks'
        }, {
            header: "排序号", width: 50, dataIndex: 'OrderNo'
        }, {
            header: "操作", width: 200, dataIndex: 'Id', renderer: function (value, a, reader, c, d, ds) {
                var links = [];
                links.push('<a href="#" onclick="var url =\'GXPackageEdit.aspx?Id=' + value + '\';window.showModalDialog(url, window, \'dialogWidth=550px;dialogHeight=450px;status=no;help=no;resizable=yes;\');Ext.getCmp(\'GXPackageMgr\').getStore().reload();return false;">编辑</a>');
                links.push('<a href="#" onclick="return delGXPackage(\'' + value + '\');">删除</a>');

                return links.join("&nbsp;");
            }
        }];
        // Set up a model to use in our Store
        Ext.define('GXPackageModel', {
            extend: 'Ext.data.Model',
            fields: [{
                name: 'Id'
            }, {
                name: 'Name'
            }, {
                name: 'IsValid'
            }, {
                name: 'Remarks'
            }, {
                name: 'OrderNo'
            }]
        });

        var ds = Ext.create('Ext.data.Store', {
            model: 'GXPackageModel',
            pageSize: 20, // items per page
            proxy: {
                type: 'ajax',
                url: '/GXPackage.mvc/Find',
                reader: {
                    type: 'json',
                    root: 'data',
                    totalProperty: 'totalCount'//分页使用
                }
            },
            autoLoad: false
        });
        ds.load({ params: { start: 0, limit: 20 } });
        this.store = ds;

        this.dockedItems = [{
            xtype: 'toolbar',
            dock: 'top',
            items: [{
                text: "添加",
                tooltip: "添加新的信息",
                iconCls: "addicon",
                handler: function () {
                    var url = 'GXPackageEdit.aspx';
                    var retVal = window.showModalDialog(url, window, 'dialogWidth=550px;dialogHeight=450px;status=no;help=no;resizable=yes;');
                    if (retVal != undefined && retVal == "Succeed") {
                        ds.reload();
                    }
                }
            }]
        }, {
            xtype: 'pagingtoolbar',
            id: 'pagingGXPackage',
            store: ds,
            dock: 'bottom',
            displayInfo: true
        }];

        // finally call the superclasses implementation
        Oa.GXPackageGrid.superclass.initComponent.call(this);
    }
});

function delGXPackage(id) {
    Ext.Msg.show({
        title: '系统确认',
        msg: '你是否确定删除该行数据?',
        buttons: Ext.Msg.YESNO,
        scope: this,
        fn: function (btn) {
            if (btn == 'yes') {
                Ext.Ajax.request({
                    url: '/GXPackage.mvc/Delete',
                    success: function (response, options) {
                        var responseMessage = Ext.JSON.decode(response.responseText);
                        if (responseMessage.Result) {
                            Ext.Msg.alert('提示', '已经成功删除数据。');
                            if (Ext.getCmp("GXPackageMgr") != null) { Ext.getCmp("GXPackageMgr").getStore().reload(); }
                        }
                        else {
                            Ext.Msg.alert('提示', '删除失败，请您重试。');
                        }
                    },
                    failure: function (action) {
                        if (action.failureType == 'server') {
                            obj = Ext.JSON.decode(action.response.responseText);
                            Ext.Msg.alert('错误:', obj.errors.reason);
                        }
                    },
                    params: {
                        id: id
                    }
                });
            }
        }
    });
    return false;
}